package cn.com.dev;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.junit.Assert;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

/**
 * @projectName: ngsapitask-sw
 * @package: cn.com.dev
 * @className: postFiles
 * @author: LiJT
 * @description: TODO
 * @date: 2022-07-13 12:02
 * @version: 1.0
 */
public class postFiles {
    private static final Log log = LogFactory.getLog(postFiles.class);

    public static void main(String[] args) throws Exception {


        // 设定服务地址
        String serverUrl = "http://xxxxxxxxxxxxxxxx";//上传地址
        String filePath = "http://xxxxxxxxxxxxxxxx";//上传地址
        String token = "http://xxxxxxxxxxxxxxxx";//上传地址
        String result = uploadFile(serverUrl, filePath, token);
        System.out.println(result);

    }


    public static String uploadFile(String url, String filePath, String token) throws Exception {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        String content = "";

        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Accept-Language", "zh-cn,zh;q=0.5");
            httpPost.setHeader("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.7");
            httpPost.setHeader("Connection", "keep-alive");
            MultipartEntity mutiEntity = new MultipartEntity();
            File file = new File(filePath);
            mutiEntity.addPart("token", new StringBody(token, StandardCharsets.UTF_8));
            mutiEntity.addPart("subject", new StringBody("123213", StandardCharsets.UTF_8));
            mutiEntity.addPart("faxnumber", new StringBody("18675571253", StandardCharsets.UTF_8));
            mutiEntity.addPart("faxnumberext", new StringBody("", StandardCharsets.UTF_8));
            mutiEntity.addPart("rawfile", new FileBody(file));
            httpPost.setEntity(mutiEntity);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            content = EntityUtils.toString(httpEntity);
            log.debug("content:" + content);
        } catch (Exception var15) {
            log.error("", var15);
        } finally {
            ;
        }

        if (httpClient != null) {
            httpClient.close();
        }

        return content;
    }
}
